{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.3.3 随机模块（numpy.random）\n",
    "import numpy as np\n",
    "import numpy.random as random\n",
    "# 设置随机种子\n",
    "random.seed(42)\n",
    "# 产生1*3 ［0，1）的随机数\n",
    "a = random.rand(3, 3)\n",
    "print(type(a))\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.43194502,  0.29122914,  0.61185289],\n       [ 0.13949386,  0.29214465,  0.36636184],\n       [ 0.45606998,  0.78517596,  0.19967378]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以下四种方式相同，都是制定大小产生［0，1）的随机数\n",
    "random.random((3, 3))\n",
    "random.sample((3, 3))\n",
    "random.random_sample((3, 3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.23225206,  4.03772426,  1.85262062,  1.32525796,  5.74442769,\n        5.82816017,  5.04198674,  2.52306885,  1.48836057,  4.42116513])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 产生10个［1，6）之间的浮点随机数\n",
    "5*random.random(10) + 1\n",
    "random.uniform(1, 6, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 3, 1, 4, 2, 4, 2, 2, 4])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 产生10个 ［1，6］ 之间的整数随机数\n",
    "random.randint(1, 6, 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.21863832,  0.88176104, -1.00908534, -1.58329421,  0.77370042],\n       [-0.53814166, -1.3466781 , -0.88059127, -1.1305523 ,  0.13442888]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2 * 5 标准正态分布样本\n",
    "random.normal(size=(2, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 5, 3, 2])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 产生5个，n = 5, p=0.5 的二项分布样本\n",
    "random.binomial(n=5, p=0.5, size=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([8, 4, 5, 3, 9, 6, 8])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机采样\n",
    "a = range(10)\n",
    "print(a)\n",
    "random.choice(a, 7)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 1 3 9 8 5 6 4 2 0]\n"
     ]
    }
   ],
   "source": [
    "# 对a 乱序 并返回一个新的arr\n",
    "b = random.permutation(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n[2, 8, 4, 1, 6, 3, 0, 9, 5, 7]\n"
     ]
    }
   ],
   "source": [
    "# 对a 乱序 in-place\n",
    "print(a)\n",
    "sh = list(a)\n",
    "random.shuffle(sh)\n",
    "print(sh)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8]\n"
     ]
    }
   ],
   "source": [
    "print(list(range(1, 9)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
